<?php
class Pay{
	var $order_sn="";
	var $payment_id="";
	var $money="";
	var $_errors = array();
    var $_errnum = 0;
    var $location="";
    //支付入口
	function pay_it($order,$info){
		$rs=$this->_set_global_var($order,$info);
		if(!$rs){
			return false;
		}
		$payment=modcall("payment","get",array("payment_id={$this->payment_id}"));

		if($payment){
            $payment["bank_code"]=$info["bank_code"];
			call_user_func_array(array($this, "_" . $payment['payment_code'] . "_pay"), array($payment));
		}
		else{
			return false;
		}

	}
	/* 生成支付记录 */
    private function madePaySign(){
        $data = array(
            'order_sn_main' => $this->order_sn,
            'out_order_sn' => '',
            'pay_id' => $this->payment_id,
            'money' => $this->money,
            'ctime' => gmtime(),
            'status' => 'ready'
        );
        return modcall("paysign","add",array($data));
    }
    function _error($msg, $obj = '')
    {
        if(is_array($msg))
        {
            $this->_errors = array_merge($this->_errors, $msg);
            $this->_errnum += count($msg);
        }
        else
        {
            $this->_errors[] = compact('msg', 'obj');
            $this->_errnum++;
        }
    }

   
    function has_error()
    {
        return $this->_errnum;
    }

  
    function get_error()
    {
        return $this->_errors;
    }
	//农行支付
	function _agri_pay(){
		include(UPATH . '/library/payments/agri/agri.payment.php');
        $agri_conf=load_conf("agri");
        $o_rs=modcall("charge_order","get",array("condition"=>"serial_number='{$this->order_sn}'",
                                "fields"=>"add_time"    
                                ));
        $order_date=date("Y/m/d",$o_rs["add_time"]);
        $order_time=date("H:i:s",$o_rs["add_time"]);
        $order_type=2;

        $retID = $this->madePaySign();
        if(!$retID){
            $this->_is_pay = false;
            return false;
        }
        $merchantPaymentRequest = new MerchantPaymentRequest(
            $this->order_sn,
            "asdfadsf",
            $order_date,
            $order_time,
            $this->money,
            $agri_conf["order_query"],
            $order_type,
            $agri_conf["payment_type"],
            $agri_conf["notify_type"],
            $agri_conf["notify_url"],
            "",
            $agri_conf["link_type"],
            array()
        );
        $merchantPayment = new MerchantPayment($agri_conf["address"],$merchantPaymentRequest);
        $merchantPaymentResult = $merchantPayment->invoke();
        if($merchantPaymentResult->isSucess==TRUE)
        {
            $this->location = $merchantPaymentResult->paymentURL;
            $this->_is_pay=true;
        }
        else
        {
            
            $this->_error(iconv("GBK","UTF-8",$merchantPaymentResult->ErrorMessage));
        }
	}

    //汇付天下支付

    //农行支付
    function _whtx_pay(){
        include(UPATH . '/library/payments/agri/agri.payment.php');
        $agri_conf=load_conf("whtx");
        $o_rs=modcall("charge_order","get",array("condition"=>"serial_number='{$this->order_sn}'",
                                "fields"=>"add_time"    
                                ));
        $order_date=date("Y/m/d",$o_rs["add_time"]);
        $order_time=date("H:i:s",$o_rs["add_time"]);
        $order_type=2;

        $retID = $this->madePaySign();
        if(!$retID){
            $this->_is_pay = false;
            return false;
        }
        $merchantPaymentRequest = new MerchantPaymentRequest(
            $this->order_sn,
            "asdfadsf",
            $order_date,
            $order_time,
            $this->money,
            $agri_conf["order_query"],
            $order_type,
            $agri_conf["payment_type"],
            $agri_conf["notify_type"],
            $agri_conf["notify_url"],
            "",
            $agri_conf["link_type"],
            array()
        );
        $merchantPayment = new MerchantPayment($agri_conf["address"],$merchantPaymentRequest);
        $merchantPaymentResult = $merchantPayment->invoke();
        if($merchantPaymentResult->isSucess==TRUE)
        {
            $this->location = $merchantPaymentResult->paymentURL;
            $this->_is_pay=true;
        }
        else
        {
            
            $this->_error(iconv("GBK","UTF-8",$merchantPaymentResult->ErrorMessage));
        }
    }

    function _alipay_pay($payment_info){

        $payment = $this->_get_payment('alipay', $payment_info);
        $payment_form = $payment->get_payform($this->order_sn, $this->money);
        $location = $payment_form['gateway'] . '?'; //网关地址
        foreach($payment_form['params'] as $key => $val){
            $location .= $key . '=' . urlencode($val) . '&';
        }
        $this->_is_pay = true;
        $this->location = substr($location, 0, -1);
    }

     /* 获取支付方式实例 */
    public function _get_payment($code, $payment_info){
        
        include_once(UPATH . '/library/payments/payment.base.php');
        include(UPATH . '/library/payments/' . $code . '/' . $code . '.payment.php');
        $class_name = ucfirst($code) . 'Payment';
        return new $class_name($payment_info);
    }

	//设置全局参数
	private function _set_global_var($order,$info){
		$this->order_sn=$order;
		foreach($info as $k=>$v){
			$this->$k=$v;
		}
		if(!$this->payment_id ||!$this->money||!$this->order_sn) return false;
		return true;

	}



}


?>